﻿using Commen;
using DAL;
using DbEntity;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;
using System.Windows.Forms;

namespace AyaWo.SpiderService
{
    public partial class LogForm : Form
    {
        //禁用窗体的关闭按钮
        private const int CP_NOCLOSE_BUTTON = 0x200;
        protected override CreateParams CreateParams
        {
            get
            {
                CreateParams myCp = base.CreateParams;
                myCp.ClassStyle = myCp.ClassStyle | CP_NOCLOSE_BUTTON;
                return myCp;
            }
        }

        public LogForm()
        {
            InitializeComponent();
            UserInfo ui = DatHelper<UserInfo>.GetDat("userInfo.dat");
            if (ui != null)
            {
                textBox1.Text = ui.userName;

                if (ui.IsAuto)
                {
                    checkBox2.Checked = true;
                    //  检查登陆合法
                    //bool logisok = UserInfoService.Loging(ui);
                    //if (logisok)
                    //{
                    //    Caches.Write<string>("userInfo", ui.userName);
                    //    DialogResult = DialogResult.OK;
                    //}
                }
                if (ui.mp)
                {
                    textBox2.Text = ui.passWorrld;
                    checkBox1.Checked = true;
                }
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            string username = textBox1.Text.Trim();
            if (username.IsNullOrWhiteSpace())
            {
                MessageBox.Show("用户名不能为空...");
                return;
            }
            string passworrd = textBox2.Text.Trim();
            if (passworrd.IsNullOrWhiteSpace())
            {
                MessageBox.Show("密码不能为空...");
                return;
            }

            bool IsAuto = checkBox2.Checked;

            UserInfo ui = new UserInfo { userName = username, passWorrld = passworrd, IsAuto = IsAuto };
            if (checkBox1.Checked)
            {
                ui.mp = true;
            }
            else
            {
                ui.mp = false;
            }
            //  检查登陆合法
            bool logisok = UserInfoService.Loging(ui);
            if (logisok)
            {
                Caches.Write<string>("userInfo", ui.userName);
                if (checkBox1.Checked == true)
                {
                    DatHelper<UserInfo>.SaveDat("userInfo.dat", fastJSON.JSON.Instance.ToJSON(ui));
                }
                else
                {
                    DatHelper<UserInfo>.SaveDat("userInfo.dat", fastJSON.JSON.Instance.ToJSON(new UserInfo() { userName = username }));
                }

                DialogResult = DialogResult.OK;
            }
            else
            {

            }
        }

        private void button9_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void checkBox2_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox2.Checked == true)
            {
                checkBox1.Checked = true;
            }
        }

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox1.Checked == false)
            {
                checkBox2.Checked = false;
            }
        }
    }


}
